<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/home.png" />
        <span>管理系统</span>
      </div>
      <el-button type="info" round @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="menuCollapse" @click="isCollapses">|||</div>
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="saveNavActive('/' + childrenItem.path)" :index="'/' + childrenItem.path" v-for="childrenItem in item.children" :key="childrenItem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{childrenItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main> 
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      icons: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'

      },
      isCollapse: false,
      activePath: ''  
    }
  },
  methods: {
    logout() {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      // console.log(res)
    },
    //菜单栏折叠
    isCollapses(){
      this.isCollapse = !this.isCollapse
    },
    // 菜单栏激活状态
    saveNavActive(activePath){
      sessionStorage.setItem('active',activePath),
      this.activePath = activePath
    }
  },
  created() {
    this.getMenuList()
  },
}
</script>

<style lang="less" scoped>
.el-header {
  background-color:#333744;
  display: flex;
  justify-content: space-between;
  color: #fff;
  font-size: 20px;
  padding-left: 5px;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: rgb(228, 222, 222);
}
.home-container {
  height: 100%;
}
.iconfont{
  margin: 5px;
}
.menuCollapse{
  line-height: 30px;
  background-color: rgb(96, 102, 100);
  font-size: 15px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
